import {messages} from "share/common";
import React from 'react'
import { connect } from 'react-redux'

import menuRoute from "routes/menuRoute";
import { Form, Button, message } from 'antd'

import ApproveBar from 'components/template/approve-bar'
import priceService from 'containers/approve/price-review/price-review.service'


class PriceReviewBtn extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      passLoading: false,
      rejectLoading: false,
      priceReviewList: menuRoute.getRouteItem('price-review','key'),
    }
  }

  //审核操作
  handleReview = (type, comment) => {
    let params = {
      applicationOID: this.props.applicationOID,
      flag: type === 'pass', //true 通过，false 驳回
      comment
    };
    this.setState({
      passLoading: type === 'pass',
      rejectLoading: type === 'reject'
    });
    priceService.priceReviewPassOrReject(params).then(() => {
      message.success('操作成功');
      this.goBack('approving')
    }).catch(() => {
      this.setState({
        passLoading: false,
        rejectLoading: false
      })
    })
  };

  //返回
  goBack = (type) => {
    this.context.router.push(this.state.priceReviewList.url + `?tab=${type}`)
  };

  render() {
    const { passLoading, rejectLoading, priceReviewList } = this.state;
    return (
      <div className="price-review-btn">
        {this.props.approving ? (
          <ApproveBar backUrl={priceReviewList.url}
                      passLoading={passLoading}
                      rejectLoading={rejectLoading}
                      handleApprovePass={(value) => this.handleReview('pass', value)}
                      handleApproveReject={(value) => this.handleReview('reject', value)}
          />
        ) : (
          <Button type="primary" onClick={() => this.goBack('approved')}>{messages('common.back')/*返回*/}</Button>
        )}
      </div>

    )
  }
}

PriceReviewBtn.contextTypes = {
  router: React.PropTypes.object
};

PriceReviewBtn.propTypes = {
  approving: React.PropTypes.bool,
  applicationOID: React.PropTypes.string
};

function mapStateToProps(state) {
  return {
    user: state.login.user
  }
}

const wrappedRefundBtns = Form.create()(PriceReviewBtn);

export default connect(mapStateToProps)(wrappedRefundBtns)
